package com.station.search.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.station.search.common.domain.WebResponse;
import com.station.search.common.domain.WebResponseBuilder;
import com.station.search.constant.DeleteEnum;
import com.station.search.domain.StationCheckRecord;
import com.station.search.domain.converter.StationCheckRecordConverter;
import com.station.search.domain.dto.StationCheckRecordDTO;
import com.station.search.domain.vo.StationCheckRecordVO;
import com.station.search.service.StationCheckRecordService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author: JT
 * @date: 2021/9/5
 * @Title:
 *         台站巡检记录 web控制器
 */
@RestController
@RequestMapping
public class StationCheckRecordController {

    @Resource
    private StationCheckRecordService stationCheckRecordService;

    @GetMapping("/stations/checkRecords")
    public WebResponse checkRecords(
            @RequestParam(value="stationCode", required = false) String stationCode,
            @RequestParam(value="searchWord", required = false) String searchWord
    ) {
        LambdaQueryChainWrapper<StationCheckRecord> stationCheckRecordsQuery = stationCheckRecordService.lambdaQuery();
        stationCheckRecordsQuery.eq(StationCheckRecord::getIsDelete, DeleteEnum.NOT_DELETED.getValue());
        if (StringUtils.hasText(stationCode)) {
            stationCheckRecordsQuery.eq(StationCheckRecord::getStationCode, stationCode);
        }
        if (StringUtils.hasText(searchWord)) {
            stationCheckRecordsQuery.and(
                    stationCheckRecordLambdaQueryWrapper ->
                        stationCheckRecordLambdaQueryWrapper.like(StationCheckRecord::getStationCode, searchWord)
                                .or()
                                .like(StationCheckRecord::getStationName, searchWord)
                                .or()
                                .like(StationCheckRecord::getCheckRecord, searchWord)
            );
        }
        stationCheckRecordsQuery.orderByDesc(StationCheckRecord::getCheckDate);
        List<StationCheckRecord> checkRecords = stationCheckRecordsQuery.list();
        return WebResponseBuilder.ok("获取巡检记录列表完成！").data(
                checkRecords.stream()
                        .map((recordDo) -> StationCheckRecordConverter.INSTANCE.doToDto(recordDo))
                        .map(record -> {
                            if(Objects.isNull(record.getCheckRecord())) {
                                return record;
                            }
                            record.setCheckRecord(record.getCheckRecord().replace("\n", ""));
                            if (record.getCheckRecord().length() <= 10) {
                                return record;
                            }
                            record.setCheckRecord(record.getCheckRecord().substring(0, 10) + "...");
                            return record;
                        })
                        .collect(Collectors.toList())
        );
    }

    @GetMapping("/stations/checkRecords/{recordId}")
    public WebResponse checkRecordById(@PathVariable("recordId") String recordId) {
        StationCheckRecord stationCheckRecord = stationCheckRecordService.getById(recordId);
        StationCheckRecordDTO stationCheckRecordDTO = StationCheckRecordConverter.INSTANCE.doToDto(stationCheckRecord);
        return WebResponseBuilder.ok(
                "获取台站[{}/{}]巡检记录[{}]!", stationCheckRecordDTO.getStationCode(), stationCheckRecordDTO.getStationName(), stationCheckRecordDTO.getId()
        ).data(stationCheckRecordDTO);
    }

    @PostMapping("/stations/checkRecords")
    public WebResponse addCheckRecord(@RequestBody StationCheckRecordVO stationCheckRecordVO) {
        StationCheckRecord stationCheckRecord = StationCheckRecordConverter.INSTANCE.voToDo(stationCheckRecordVO);
        stationCheckRecord.setCheckPeople(
                stationCheckRecord.getCheckPeople()
                        .stream()
                        .filter(item -> StringUtils.hasText(item.getText()))
                        .collect(Collectors.toList())
        );
        stationCheckRecordService.saveOrUpdate(stationCheckRecord);
        if (StringUtils.isEmpty(stationCheckRecordVO.getId())) {
            return WebResponseBuilder.ok("添加台站[{}]巡检记录[{}]成功！", stationCheckRecord.getStationCode(), stationCheckRecord.getId());
        }
        return WebResponseBuilder.ok("修正台站[{}]巡检记录[{}]成功！", stationCheckRecord.getStationCode(), stationCheckRecord.getId());
    }

    @PutMapping("/stations/checkRecords/{recordId}")
    public WebResponse modifyCheckRecord(@PathVariable("recordId")String recordId, @RequestBody StationCheckRecordVO stationCheckRecordVO) {
        StationCheckRecord stationCheckRecord = StationCheckRecordConverter.INSTANCE.voToDo(stationCheckRecordVO);
        stationCheckRecord.setId(recordId);
        stationCheckRecordService.saveOrUpdate(stationCheckRecord);
        return WebResponseBuilder.ok("修改台站[{}]巡检记录成功！");
    }

    @GetMapping("/stations/{stationId}/checkRecords")
    public WebResponse queryStationCheckRecords(@PathVariable("stationId") String stationId) {
        List<StationCheckRecord> stationCheckRecords = stationCheckRecordService.queryByStationId(stationId);
        return WebResponseBuilder.ok("查询台站[{}]巡检记录成功！")
                .data(stationCheckRecords);
    }

    @DeleteMapping("/stations/checkRecords/{recordId}")
    public WebResponse deleteStationCheckRecord(@PathVariable("recordId")String recordId) {
        stationCheckRecordService.deleteStationCheckRecordById(recordId);
        return WebResponseBuilder.ok("删除巡检记录成功！[记录ID: {}]", recordId);
    }
}
